package com.benyi.byiee.rest;

import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestHeader;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.benyi.byiee.constants.Role;
import com.benyi.byiee.constants.UserType;
import com.benyi.byiee.model.db.LanguageInfo;
import com.benyi.byiee.service.LanguageInfoService;
import com.benyi.util.JsonResult;
import com.benyi.util.bean.ServiceException;

@RestController
@RequestMapping("/lan")
public class LanguageInfoRest {
private static Logger log = LoggerFactory.getLogger(LanguageInfoRest.class);
	
	@Autowired
	LanguageInfoService service;
	
	@RequestMapping(value = {"/ql/{lcode}","/ql","/ql/"}, method = {RequestMethod.GET,RequestMethod.POST})   
	public JsonResult<List<LanguageInfo>> queryList(@PathVariable(value="lcode",required = false) String code ){ 
		List<LanguageInfo> list = service.queryList(code);
		JsonResult<List<LanguageInfo>> r = new JsonResult<List<LanguageInfo>>();
		r.setData(list);
		r.setStatus(JsonResult.SUCCESS);
		return r;
	}
	
	@Role(value=UserType.SYSTEM) 
	@RequestMapping(value = "/sv", method = RequestMethod.POST,consumes = {"application/json"})
	public JsonResult<LanguageInfo> save(@RequestHeader String token, @RequestBody LanguageInfo req ){ 
		try {
			LanguageInfo v = service.save(req);
			return JsonResult.success(v, "sucess");
		}catch(ServiceException e) {
			return JsonResult.fail(e.getErrorCode(), e.getMessage());
		} 
	} 
	
	@Role(value=UserType.SYSTEM) 
	@RequestMapping(value = "/del", method = RequestMethod.POST,consumes = {"application/json"})
	public JsonResult<LanguageInfo> delOne(@RequestHeader String token, @RequestBody LanguageInfo v ){  
		try {
			LanguageInfo a = service.selectByCode(v.getLcode());
			if(a==null) {
				return JsonResult.fail("none","Data is not exist!");
			}
			service.delete(v);
			LanguageInfo b = service.selectByCode(v.getLcode());
			if(b!=null) {
				return JsonResult.fail("none","Delete failed!");
			}else {
				return JsonResult.success(a,"Delete succeed!");
			} 
		}catch(ServiceException e) {
			return JsonResult.fail(e.getErrorCode(),e.getMessage());
		}
	}
}
